package com.oocl.MyHttpServer.parser.impl;

import java.io.IOException;
import java.io.OutputStream;
import java.util.Iterator;
import java.util.List;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.output.SAXOutputter;
import org.jdom.output.XMLOutputter;

import com.oocl.MyHttpServer.parser.HtmlParser;
import com.oocl.qq.pojo.User;

public class HtmlParserImpl implements HtmlParser {

	private List<User> userList;
	private long curTime;
	private long serverOpenTime;

	public HtmlParserImpl setOnlineList(List<User> userList) {
		System.out.println("setOnlineList(), userList长度为： "+ userList==null?"null": userList.size());
		this.userList = userList;
		return this;
	}

	// 设置当前时间
	public HtmlParserImpl setCurTime(long curTime) {
		this.curTime = curTime;
		return this;
	}

	// 设置服务器开启了多长时间
	public HtmlParserImpl setServerOpenTime(long serverOpenTime) {
		this.serverOpenTime = serverOpenTime;
		return this;
	}

	@Override
	public void genericHtml(boolean isOpened, OutputStream out) {

		// /首先，根据当前服务端的状态，返回不同的消息。
		XMLOutputter outputter = new XMLOutputter();
		Element root = null;
		if (isOpened) {
			// 传一个有用户列表的html
			root = dealWithOpen();
		} else {
			// 传一个 只有两个Button的html
			root = dealWithClose();
		}

		try {
			System.out.println("回传响应给浏览器");
			out.write("HTTP/1.1 200 OK\r\n\r\n".getBytes());
			outputter.output(root, out);
			out.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private Element dealWithOpen() {
		Element root = new Element("html");
		Element body = new Element("body");
		body.setAttribute("margin", "auto");
		
		Element div = new Element("div");
		div.setAttribute("width","100%");
		div.setAttribute("style","text-align:center");
		
		Element formA = new Element("form");
		formA.setAttribute("action","http://localhost:9000/");
		formA.setAttribute("method","POST");
		
		Element btnOpen = new Element("input");
		
		btnOpen.setAttribute("style","font-size:24px");
		btnOpen.setAttribute("type","submit");
		btnOpen.setAttribute("value","启动");
		btnOpen.setAttribute("name","open");
		Element labelOpen = new Element("label");
		labelOpen.setAttribute("name","action");
		labelOpen.setAttribute("value","open");
		formA.addContent(labelOpen);
		formA.addContent(btnOpen);

		
		Element formB = new Element("form");
		formB.setAttribute("action","http://localhost:9000/");
		formB.setAttribute("method","POST");
		Element btnClose = new Element("input");
		
		btnClose.setAttribute("style","font-size:24px");
		btnClose.setAttribute("type","submit");
		btnClose.setAttribute("value","关闭");
		btnClose.setAttribute("name","close");
		formB.addContent(btnClose);
		
		
		Element serverStatus = new Element("h1");
		serverStatus.setText("服务器状态： 开启");
		serverStatus.setAttribute("style", "color:blue");
		serverStatus.setAttribute("margin", "auto");
		
		Element userTable = new Element("table");
		userTable.setAttribute("border","10");
		Element userListHead = new Element("th");
		Element userNameLb = new Element("td");
		Element userIsOnlineLb = new Element("td");
		userNameLb.setText("用户名");
		userIsOnlineLb.setText("是否在线");
		
		userListHead.addContent(userNameLb);
		userListHead.addContent(userIsOnlineLb);
		userTable.addContent(userListHead);

		
		for(User item: this.userList){
			Element line = new Element("tr");
			Element userName = new Element("td");
			userName.setText(item.getUserName());
			Element userIsOnline = new Element("td");
			userIsOnline.setText(item.isOnline()+"");
			line.addContent(userName);
			line.addContent(userIsOnline);
			userTable.addContent(line);
		}
		
		div.addContent(formA);
		div.addContent(formB);
		div.addContent(serverStatus);
		div.addContent(userTable);

		body.addContent(div);
		root.addContent(body);
		
		return root;
	}

	private Element dealWithClose() {
		Element root = new Element("html");
		Element body = new Element("body");
		
		Element div = new Element("div");
		div.setAttribute("width","100%");
		div.setAttribute("style","text-align:center");

		
		Element formA = new Element("form");
		formA.setAttribute("action","http://localhost:9000/");
		formA.setAttribute("method","POST");
		
		Element btnOpen = new Element("input");
		
		btnOpen.setAttribute("style","font-size:24px");
		btnOpen.setAttribute("type","submit");
		btnOpen.setAttribute("value","启动");
		btnOpen.setAttribute("name","open");
		formA.addContent(btnOpen);

		
		Element formB = new Element("form");
		formB.setAttribute("action","http://localhost:9000/");
		formB.setAttribute("method","POST");
		Element btnClose = new Element("input");
		
		btnClose.setAttribute("style","font-size:24px");
		btnClose.setAttribute("type","submit");
		btnClose.setAttribute("value","关闭");
		btnClose.setAttribute("name","close");
		formB.addContent(btnClose);
		
		Element serverStatus = new Element("h1");
		serverStatus.setText("服务器状态： 关闭");
		serverStatus.setAttribute("style", "color:red");
		serverStatus.setAttribute("margin", "auto");
		
		div.addContent(formA);
		div.addContent(formB);
		div.addContent(serverStatus);
		
		body.addContent(div);
		root.addContent(body);
		
		return root;
	}

}
